//
//  ChatViewController.swift
//  PlayWithXMPP_Swift
//
//  Created by admin on 15/1/27.
//  Copyright (c) 2015年 com.zhongyan. All rights reserved.
//

import UIKit

typealias TableViewReloadClosure = ()->Void

class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var toolBarView: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var messageTF: UITextField!
    
    var messageHelper:MessageHelper?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupTableView()
        self.setupTextField()
        self.setupXMPPStream()
       
    }
    
    func setupXMPPStream() {
        messageHelper = MessageHelper()
        messageHelper!.tableViewReloadClosure = {
            self.tableView!.reloadData()
        }
        AppDelegate.shareInstance().xmppStream!.addDelegate(messageHelper, delegateQueue: dispatch_get_main_queue())
    }
    func setupTableView() {
        self.tableView!.delegate = self;
        self.tableView!.dataSource = self;
        self.tableView!.registerNib(UINib(nibName: "OtherChatCell", bundle: nil), forCellReuseIdentifier: "OtherChatCell")
        self.tableView!.registerNib(UINib(nibName: "MyChatCell", bundle: nil), forCellReuseIdentifier: "MyChatCell")
        self.tableView!.registerNib(UINib(nibName: "ChatTimeCell", bundle: nil), forCellReuseIdentifier: "ChatTimeCell")
    }
    
    func setupTextField() {
        self.messageTF!.delegate = self
    }
    
    
    func textFieldDidBeginEditing(textField: UITextField) {
        self.tableView!.contentOffset = CGPointMake(0, 200)
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messageHelper!.receiveMsgArray.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let receiveModel:ChatInfoModel = messageHelper!.receiveMsgArray[indexPath.row]
        let cell:OtherChatCell = tableView.dequeueReusableCellWithIdentifier("OtherChatCell") as OtherChatCell
        
//        cell.chatInfoModel = receiveModel
        cell.messageLabel!.text = receiveModel.message
        
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
